package kevin.framework.basic.common.utils;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.core.io.Resource;

/**
 * @ClassName: SysConfigParams
 * @Description: TODO
 * @author kevin.huang
 * @date 2016年4月23日 下午1:46:31
 */
public class SysConfigParams {

    /** 是否默认路径 **/
    public static boolean isDefaultClassPath = true;
    /** 配置文件的根目录 ****/
    public static String configHome;

    public static String jdbcFile = "jdbc.properties";

    public static Resource JdbcResouce;

    public static String lang = "zh";

    public static String configInfo() {
        return "configHome=" + configHome + ";jdbcFile=" + jdbcFile;
    }

    /**
     * 获取模板文件夹路径
     * 
     * @return
     */
    public static String getTemplateDir() {
        return configHome + "template" + File.separator;
    }

    public static Map<String, Set<String>> exceptValidationMap = new HashMap<String, Set<String>>();

    /**
     * 可直接访问的controller/cmd集合
     **/
    public static Map<String, Set<String>> accessMap = new HashMap<String, Set<String>>();

    public static boolean canAccess(String controller, String cmd) {
        Set<String> cmdSet1 = accessMap.get("*");
        if (cmdSet1 != null) {
            if (cmdSet1.contains(cmd)) {
                return true;
            }
        }
        Set<String> cmdSet = accessMap.get(controller);
        if (cmdSet == null) {
            return false;
        }
        if (cmdSet.contains("*") || cmdSet.contains(cmd)) {
            return true;
        }
        return false;
    }

    public static void setAccess(String controller, Set<String> cmdSet) {
        if (accessMap.get(controller) == null) {
            accessMap.put(controller, cmdSet);
        }
    }

    public static void setAccess(String controller, String cmd) {
        Set<String> cmdSet = accessMap.get(controller);
        if (cmdSet == null) {
            cmdSet = new HashSet<String>();
            accessMap.put(controller, cmdSet);
        }
        cmdSet.add(cmd);
    }

    /***
     * 判断当前请求是否需要权限验证
     ***/
    public static boolean exceptValidation(String controller, String cmd) {
        Set<String> cmdSet1 = exceptValidationMap.get("*");
        if (cmdSet1 != null) {
            if (cmdSet1.contains(cmd)) {
                return true;
            }
        }
        Set<String> cmdSet = exceptValidationMap.get(controller);
        if (cmdSet == null) {
            return false;
        }
        if (cmdSet.contains("*") || cmdSet.contains(cmd)) {
            return true;
        }
        return false;
    }

    /**
     * @Description: 获取通行的controller
     * @return
     */
    public static Set<String> getNoValidController() {
        Set<String> set = new HashSet<String>();
        for (Entry<String, Set<String>> entry : exceptValidationMap.entrySet()) {
            if (entry.getValue().contains("*")) {
                set.add(entry.getKey());
            }
        }
        return set;
    }

    public static void setExceptValidation(String controller, String cmd) {
        Set<String> cmdSet = exceptValidationMap.get(controller);
        if (cmdSet == null) {
            cmdSet = new HashSet<String>();
            exceptValidationMap.put(controller, cmdSet);
        }
        cmdSet.add(cmd);
    }

    public static String webHttpContext;

    /**
     * 是否开发模式
     */
    public static boolean isDevModel;

    /**
     * cookie加密
     */
    public static String desKey;
    /**
     * cookiekey
     */
    public static String cookieKey;

    private static Map<String, String> configMap = new HashMap<String, String>();

    public static String getConfig(String key) {
        return configMap.get(key);
    }

    public static void setConfig(String key, String value) {
        if (!configMap.containsKey(key)) {
            configMap.put(key, value);
        }
    }
}
